/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package futbol.tacticas.concursantes.Potrosa;

import java.util.*;
import java.awt.Point;

/**
 *
 * @author IGadea
 */
public class MathVector {
    private double alfa = 1000.0;
    private double mod = 0.0;
    private double px = 0.0;
    private double py = 0.0;
    private double nx = 1.0;
    private double ny = 0.0;
    private double dx = 0.0;
    private double dy = 0.0;

    public MathVector(MathVector mv) {
        alfa = mv.alfa;
        mod = mv.mod;
        px = mv.px;
        py = mv.py;
        nx = mv.nx;
        ny = mv.ny;
        dx = mv.dx;
        dy = mv.dy;
    }

    public MathVector() {
    }
    
    public MathVector(double angulo) {
        mod = 1;
        cambiarDireccion(angulo);
    }

    public MathVector(double angulo, double modulo) {
        mod = modulo;
        cambiarDireccion(angulo);
    }

    public MathVector(Point a, Point b) {
        cambiarOrigen((double)a.x, (double)a.y, (double)b.x, (double)b.y);
    }

    public MathVector(int ax, int ay, int bx, int by) {
        cambiarOrigen((double)ax, (double)ay, (double)bx, (double)by);
    }

    public MathVector(double ax, double ay, double bx, double by) {
        cambiarOrigen(ax, ay, bx, by);
    }

    public double direccion() {
        if (alfa > 10.0) {      // 10 > 2*PI
            if (nx > 0)      alfa = corregirAngulo(Math.atan(ny/nx));
            else if (nx < 0) alfa = corregirAngulo(Math.atan(ny/nx) + Math.PI);
            else if (ny > 0) alfa = Math.PI/2;
            else             alfa = 3*Math.PI/2;
        }
        return alfa;
    }

    public double tamanyo() {
        return mod;
    }

    public double origenX() {
        return px;
    }

    public double origenY() {
        return py;
    }

    public double destinoX() {
        return dx;
    }

    public double destinoY() {
        return dy;
    }

    public double vectorX() {
        return nx;
    }

    public double vectorY() {
        return ny;
    }

    public void girarAngulo(double angulo) {
        actualizarAngulo(direccion() + angulo);
    }

    public void cambiarDireccion(double angulo) {
        actualizarAngulo(angulo);
    }
    
    private void actualizarAngulo(double angulo) {
        alfa = corregirAngulo(angulo);
        nx = Math.cos(alfa);
        ny = Math.sin(alfa);
        dx = px + nx * mod;
        dy = py + ny * mod;
    }

    // Proviene de Futbol.General.java
    private double corregirAngulo(double angulo) {
        while (angulo < 0)
            angulo += Math.PI*2;
        while (angulo >= Math.PI*2)
            angulo -= Math.PI*2;
        return angulo;
    }

    public void desplazarVector(double ax, double ay) {
        px += ax;
        py += ay;
        dx += ax;
        dy += ay;
    }

    public void cambiarTamanyo(double tamanyo) {
        mod = tamanyo;
        dx = px + mod*nx;
        dy = py + mod*ny;
    }
    
    public void cambiarOrigen(double ax, double ay, double bx, double by) {
        double sx = bx - ax;
        double sy = by - ay;
        mod = Math.sqrt(sx*sx + sy*sy);
        px = ax;
        py = ay;
        dx = bx;
        dy = by;
        if (mod == 0.0) {
            nx = ny = 0.0;
        } else {
            nx = sx / mod;
            ny = sy / mod;
        }
        alfa = 1000;
    }
}
